package com.hknc.btscale;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import android.os.Bundle;

import com.hknc.btscale.bean.GoodsBean;
import com.hknc.btscale.bean.SaveGoods;
import com.hknc.btscale.utils.SPUtils;
import com.zhuang.zbannerlibrary.ZBanner;
import com.zhuang.zbannerlibrary.ZBannerAdapter;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class TestActivity extends AppCompatActivity {
    private ZBanner mZBanner;
    private int part1, part2;
    private LinkedHashMap linkedHashMap;
    private List<GoodsBean.DataBean> mDataBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        GoodsBean.DataBean dataBean1 = new GoodsBean.DataBean();
        mDataBeanList = new ArrayList<>();
        dataBean1.setGoodsName("litepal.xml");
        GoodsBean.DataBean dataBean2 = new GoodsBean.DataBean();
        dataBean2.setGoodsName("bbb");
        GoodsBean.DataBean dataBean3 = new GoodsBean.DataBean();
        dataBean3.setGoodsName("ccc");
        mDataBeanList.add(dataBean1);
        mDataBeanList.add(dataBean2);
        mDataBeanList.add(dataBean3);
        linkedHashMap = groupList(mDataBeanList);
        part1 = linkedHashMap.size();
        linkedHashMap.put(part1, SPUtils.getInstance(getBaseContext()).getSP("businessLicensePic"));
        linkedHashMap.put(part1 + 1, SPUtils.getInstance(getBaseContext()).getSP("circulationLicense"));

        mZBanner = findViewById(R.id.zbanner);
        mZBanner.setAdapter(new MyBannerAdapter(getSupportFragmentManager(), linkedHashMap));

    }

    @Override
    protected void onResume() {
        super.onResume();
        mZBanner.star(4000, 2000);
    }

    private class MyBannerAdapter extends ZBannerAdapter {
        private LinkedHashMap mLinkedHashMap;

        public MyBannerAdapter(FragmentManager fm, LinkedHashMap list) {
            super(fm);
            mLinkedHashMap = list;
        }

        @Override
        public Fragment getItem(int position) {
            if (mLinkedHashMap == null)
                return null;
            if (position < part1) {
                return MainActivity.BannerFragmentRv.newInstance1(position, (List<SaveGoods>) mLinkedHashMap.get(position));
            }
            return MainActivity.BannerFragment.newInstance1(position, (String) mLinkedHashMap.get(position));
        }

        @Override
        public int getCount() {
            return mLinkedHashMap == null ? 0 : mLinkedHashMap.size();
        }
    }

    /**
     * 实现java 中 list集合中有几十万条数据,每100条为一组取出
     *
     * @param list 可穿入几十万条数据的List
     * @return map 每一Kye中有100条数据的List
     */
    private LinkedHashMap groupList(List<GoodsBean.DataBean> list) {

        int listSize = list.size();
        int toIndex = 15;
        LinkedHashMap map = new LinkedHashMap();     //用map存起来新的分组后数据
        int keyToken = 0;
        for (int i = 0; i < list.size(); i += 15) {
            if (i + 15 > listSize) {        //作用为toIndex最后没有100条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List newList = list.subList(i, i + toIndex);
            map.put(keyToken, newList);
            keyToken++;
        }

        return map;
    }
}